﻿#include "ippage.h"
#include "u_ip_edit.h"

IpPage::IpPage(QWidget *parent) : MyDialog(parent)
{
    this->InitWidget();
    this->SetupConnections();
    this->Initialize();

    this->setMaxHide();
    this->setTitle("网口管理");
    this->resize(800, 400);
}

void IpPage::InitWidget(){
    btnAdd  = new QPushButton(QChar(0xe722) + QString(" 添加"));
    btnDel  = new QPushButton(QChar(0xe603) + QString(" 删除"));

    QStringList headers = QString("功率,IP地址,端口号").split(",");
    tableWidget = MyGUI::GetTableWidget(headers, 120, false);
    tableWidget->setColumnWidth(1, 200);
    tableWidget->horizontalHeader()->setStretchLastSection(true);
    tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addStretch();
    btnLayout->addWidget(btnAdd);
    btnLayout->addWidget(btnDel);

    QGroupBox *groupbox11 = new QGroupBox();
    groupbox11->setStyleSheet("margin-top: 0px;");
    groupbox11->setLayout(btnLayout);

    //-------------------------------------------------

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setMargin(0);
    mainLayout->setSpacing(10);
    if(globalData->CheckUserRole(0)){
        mainLayout->addWidget(groupbox11);
    }
    mainLayout->addWidget(tableWidget);
    this->addLayout(mainLayout);
}

void IpPage::SetupConnections(){
    connect(btnAdd , SIGNAL(clicked()), this, SLOT(OnBtnAddClicked()));
    connect(btnDel , SIGNAL(clicked()), this, SLOT(OnBtnDelClicked()));
    connect(tableWidget,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(OnTableDoubleClicked(int,int)));
}

void IpPage::Initialize(){
    queryList = globalData->sqlBase->GetIpList();
    this->UpdateTable();
}

void IpPage::UpdateTable(){
    int count = queryList.size();

    tableWidget->setRowCount(0);
    tableWidget->setRowCount(count);

    for(int i=0; i<count; i++){
        IPData info = queryList.at(i);

        tableWidget->setItem(i, 0, MyGUI::GetTableWidgetItem(info.Kw));
        tableWidget->setItem(i, 1, MyGUI::GetTableWidgetItem(info.IP));
        tableWidget->setItem(i, 2, MyGUI::GetTableWidgetItem(info.Port));
        //tableWidget->setItem(i, 7, MyGUI::GetTableWidgetItem(info.enable ? "启用" : "禁用", Qt::AlignCenter, info.enable ? Qt::green : Qt::red));
    }
}

void IpPage::OnBtnAddClicked(){
    IPData info;
    (new IpEdit(info, this))->exec();
    this->Initialize();
}

void IpPage::OnBtnDelClicked(){
    int row = tableWidget->currentRow();
    if(row < 0){
        MyGUI::ShowMessageBoxError("请选择要删除的记录", this);
        return;
    }
    if(!MyGUI::ShowMessageBoxQuesion("确定要删除选中的记录？", this)){
        return;
    }

    IPData info = queryList.at(row);
    bool b = globalData->sqlBase->DelIPData(info.IP);

    if(b){
        this->Initialize();
        MyGUI::ShowMessageBoxInfo("删除成功", this);
    }else{
        MyGUI::ShowMessageBoxError("删除失败", this);
    }
}

void IpPage::OnTableDoubleClicked(int row, int column){
    IPData info = queryList.at(row);
    (new IpEdit(info, this))->exec();
    this->Initialize();
}
